import { reqCoodsInfo, reqAddOrUpdateShopCart } from "@/api";
import { getUUID } from '@/utils/uuid_token'
const state = {
  goodInfo: {},
  // 游客临时身份 生成随机字符串
  uuid_token: getUUID()
};
const mutations = {
  GETGOODINFO (state, goodInfo) {
    state.goodInfo = goodInfo
  }
}
const actions = {
  // 获取产品信息
  async getGoodInfo ({ commit }, skuid) {
    let result = await reqCoodsInfo(skuid)
    if (result.code == 200) {
      commit('GETGOODINFO', result.data)
    }
  },
  // 把物品添加到购物车中 、修改物品数量
  async addOrUpdateShopCart ({ commit }, { skuId, skuNum }) {
    // 只会返回 200 提示 
    let result = await reqAddOrUpdateShopCart(skuId, skuNum)
    if (result.code == 200) {
      return 'OK'
    } else {
      return Promise.reject(new Error('faile'))
    }

  }
};
const getters = {
  // 简化 导航栏
  categoryView (state) {
    // {} 预防警告
    return state.goodInfo.categoryView || {}
  },
  // 简化 产品信息
  skuInfo (state) {
    return state.goodInfo.skuInfo || {}
  },
  // 简化 产品属性
  spuSaleAttrList (state) {
    return state.goodInfo.spuSaleAttrList || [];
  }
};
export default {
  state,
  actions,
  mutations,
  getters
}
